package com.ruoyi.mdm.utils;

import com.ruoyi.mdm.constant.MdmConstants;

import jakarta.mail.Folder;
import jakarta.mail.Message;
import jakarta.mail.Session;
import jakarta.mail.Store;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class EmailUtil {

    /**
     * 读取邮箱内容
     *
     * @param host 服务器地址
     * @param port 端口
     * @param protocol protocol
     * @param useSsl 是否启用ssl
     * @param userName 邮箱地址
     * @param passwd 密码
     * @return 邮件标题
     */
    public static List<Message> receiveEmail(String host,
                                             String port,
                                             String protocol,
                                             String useSsl,
                                             String userName,
                                             String passwd,
                                             Date lastDate
                                      ) {

        // pop3配置，可保存到properties文件，读取
        Properties props = new Properties();

        // POP3设置方法
        if (MdmConstants.EMAIL_PROTOCOL_POP3.equals(protocol)) {

            props.put("mail.pop3.host", host);
            props.put("mail.pop3.port", port);
            if ("1".equals(useSsl)) {
                props.put("mail.pop3.ssl", true);
            }

            // 需要认证
            props.put("mail.pop3.auth", true);
            props.put("mail.pop3.user", userName);
            props.put("mail.pop3.pass", passwd);

            // 使用ssl
            if ("1".equals(useSsl)) {
                props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.pop3.socketFactory.fallback", false);
                props.put("mail.pop3.socketFactory.port", port);
            }

        } else if (MdmConstants.EMAIL_PROTOCOL_IMAP.equals(protocol)) {

            props.put("mail.imap.host", host);
            props.put("mail.imap.port", port);
            if ("1".equals(useSsl)) {
                props.put("mail.imap.ssl", true);
            }

            // 需要认证
            props.put("mail.imap.auth", true);
            props.put("mail.imap.user", userName);
            props.put("mail.imap.pass", passwd);

            // 使用ssl
            if ("1".equals(useSsl)) {
                props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.imap.socketFactory.fallback", false);
                props.put("mail.imap.socketFactory.port", port);
            }
        }

        try {
            Session session = Session.getDefaultInstance(props);
            Store store = session.getStore(protocol);
            // 需要认证
            store.connect(host, Integer.parseInt(port),
                    userName,
                    passwd);

            // 获取收件箱
            Folder mbox = store.getFolder("INBOX");
            // INBOX
            mbox.open(Folder.READ_ONLY);
            System.out.println(mbox.getName());

            int msgCount = mbox.getMessageCount();

            List<Message> messageList = new ArrayList<>();

            for (int i = msgCount; i > 0; i--) {

                Message msg = mbox.getMessage(i);

                if (lastDate == null) {
                    messageList.add(msg);
                } else {
                    if (msg.getSentDate().compareTo(lastDate) > 0) {
                        messageList.add(msg);
                    } else {
                        break;
                    }
                }
            }

            return messageList;

        } catch (Exception e) {
            System.out.println("读取邮箱异常:" + e);
        }

        return null;
    }

//    public static void main(String args[])  {
//
//        receiveEmail("pop.qq.com",
//                "995",
//                "pop3",
//                "1",
//                "21716567@qq.com",
//                "pwdztdpeivrbcaid",
//                new Date());
//
////        receiveEmail("imap.qq.com",
////                "993",
////                "imap",
////                "1",
////                "21716567@qq.com",
////                "pwdztdpeivrbcaid",
////                new Date());
//
////        receiveEmail("pop3.isoftstone.com",
////                "995",
////                "pop3",
////                "1",
////                "nbliuc@isoftstone.com",
////                "isoftStone@#4567",
////                new Date());
//
//
//        System.out.println("邮件主题：");
//    }
}